class IUP_GET_COLOR
-- Shows a modal dialog which allows the user to select a color. Based on 
-- IUP_COLOR_DIALOG.
--
-- The dialog uses a global attribute called "PARENTDIALOG" as the parent 
-- dialog if it is defined. It also uses a global attribute called "ICON" as 
-- the dialog icon if it is defined.	

inherit
	IUP_WIDGET

create {ANY}
   get_color_with_initial

feature {ANY}

	get_color_with_initial (red, green, blue: INTEGER)
		-- Create a color dialog with the specified color selected when 
		-- the dialog is shown.
		require
			red >= 0
			red <= 255
			green >= 0
			green <= 255
			blue >= 0
			blue <= 255
		do
			r := red
			g := green
			b := blue
		end

	launch_predefined_xy (x, y: STRING): TUPLE[INTEGER, INTEGER, INTEGER, INTEGER]
		-- Show the dialog using predefined values for its position. However 
		-- you can combine a predefined value with an integer (as string). 
		-- Return a tuple with four integer values. The first is 1 if the OK 
		-- button is pressed, 0 otherwise. The rest are the RGB values of the 
		-- selected color.	
		--
		-- x: Predefined horizontal position. The following definitions 
		-- can be used:
		--
		-- IUP_LEFT: Positions the element on the left corner of the main screen.
		-- IUP_CENTER: Centers the element on the main screen.
		-- IUP_RIGHT: Positions the element on the right corner of the main screen
		-- IUP_MOUSEPOS: Positions the element on the mouse cursor
		-- IUP_CENTERPARENT: Horizontally centralizes the dialog relative to its 
		-- parent. (Since 3.0)
		-- IUP_CURRENT: use the current position of the dialog.. (Since 3.0)
		--
		-- y: Predefined vertical position. The following definitions 
		-- can be used:
		--
		-- IUP_TOP: Positions the element on the top of the main screen
		-- IUP_CENTER: Vertically centers the element on the main screen
		-- IUP_BOTTOM: Positions the element on the base of the main screen
		-- IUP_MOUSEPOS: Positions the element on the mouse cursor
		-- IUP_CENTERPARENT: Vertically centralizes the dialog relative to its 
		-- parent. (Since 3.0)
		-- IUP_CURRENT: use the current position of the dialog. (Since 3.0)
		require
			is_valid_position(x, y)
		do
			Result := launch(iup_open.position_to_integer(x),
								  iup_open.position_to_integer(y))
		end

	launch (x, y: INTEGER): TUPLE[INTEGER, INTEGER, INTEGER, INTEGER]
		-- Show the dialog. Return a tuple with four integer values. The first 
		-- is 1 if the OK button is pressed, 0 otherwise. The rest are the 
		-- RGB values of the selected color.
		--
		-- x: horizontal position of the top left corner of the window, 
		-- relative to the origin of the main screen.
		--
		-- y: vertical position of the top left corner of the window or menu, 
		-- relative to the origin of the main screen.
		local
			sr, sg, sb, otr, otg, otb: STRING
			pr, pg, pb: POINTER
			i: INTEGER
			tup: TUPLE[INTEGER, INTEGER, INTEGER, INTEGER]
		do
			create sr.make(10)
         create sg.make(10)
         create sb.make(10)

			sr.append_string(r.out)
			sg.append_string(g.out)
			sb.append_string(b.out)

			pr := get_pointer(sr.to_c)
			pg := get_pointer(sg.to_c)
			pb := get_pointer(sb.to_c)

			i := int_get_color (x, y, pr, pg, pb)

			create otr.make_from_c(pr)
			create otg.make_from_c(pg)
			create otb.make_from_c(pb)

			if otr.is_empty then
				otr.append_string("0")
			end

			if otg.is_empty then
				otg.append_string("0")
			end

			if otb.is_empty then
				otb.append_string("0")
			end

			tup := [i, otr.item(1).code,
			        otg.item(1).code,
                 otb.item(1).code]
					  
		  Result := tup
		end

	-- Validations

	is_valid_position (x, y: STRING): BOOLEAN
		local
			xs, ys: BOOLEAN
		do
			if x.is_equal("IUP_LEFT") or
				x.is_equal("IUP_CENTER") or
				x.is_equal("IUP_RIGHT") or
				x.is_equal("IUP_MOUSEPOS") or
				x.is_equal("IUP_CENTERPARENT") or
				x.is_equal("IUP_CURRENT") then
				xs := True
			elseif x.is_integer and x.to_integer >= 0 then
				xs := True
			else
				xs := False
			end

			if y.is_equal("IUP_TOP") or
				y.is_equal("IUP_CENTER") or
				y.is_equal("IUP_BOTTOM") or
				y.is_equal("IUP_MOUSEPOS") or
				y.is_equal("IUP_CENTERPARENT") or
				y.is_equal("IUP_CURRENT") then
				ys := True
			elseif y.is_integer and y.to_integer >= 0 then
				ys := True
			else
				ys := False
			end

			if xs and ys then
				Result := True
			else
				Result := False
			end
		end

feature {NONE}

	r, g, b: INTEGER

	-- Internal

	int_get_color (x, y: INTEGER; rv, gv, bv: POINTER): INTEGER
		external
			"C inline use %"eiffel-iup.h%""
      alias
			"return IupGetColor ($x, $y, $rv, $gv, $bv);"
      end

end -- end IUP_GET_COLOR

-- The MIT License (MIT)

-- Copyright (c) 2016, 2017, 2019 by German A. Arias

-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in 
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
